package com.lzc.service.impl;

import com.lzc.service.TranslationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 翻译服务实现类
 * 这里提供简单的映射翻译，实际项目中可以集成百度翻译、谷歌翻译等API
 */
@Slf4j
@Service
public class TranslationServiceImpl implements TranslationService {

    // 简单的中英文映射表，实际项目中应该从数据库或配置文件加载
    private static final Map<String, String> CHINESE_TO_ENGLISH = new HashMap<>();
    private static final Map<String, String> ENGLISH_TO_CHINESE = new HashMap<>();
    
    static {
        // 省份中英文映射
        CHINESE_TO_ENGLISH.put("北京", "Beijing");
        CHINESE_TO_ENGLISH.put("上海", "Shanghai");
        CHINESE_TO_ENGLISH.put("广东", "Guangdong");
        CHINESE_TO_ENGLISH.put("江苏", "Jiangsu");
        CHINESE_TO_ENGLISH.put("浙江", "Zhejiang");
        CHINESE_TO_ENGLISH.put("山东", "Shandong");
        CHINESE_TO_ENGLISH.put("河南", "Henan");
        CHINESE_TO_ENGLISH.put("四川", "Sichuan");
        CHINESE_TO_ENGLISH.put("湖北", "Hubei");
        CHINESE_TO_ENGLISH.put("福建", "Fujian");
        CHINESE_TO_ENGLISH.put("湖南", "Hunan");
        CHINESE_TO_ENGLISH.put("安徽", "Anhui");
        CHINESE_TO_ENGLISH.put("河北", "Hebei");
        CHINESE_TO_ENGLISH.put("陕西", "Shaanxi");
        CHINESE_TO_ENGLISH.put("辽宁", "Liaoning");
        CHINESE_TO_ENGLISH.put("江西", "Jiangxi");
        CHINESE_TO_ENGLISH.put("重庆", "Chongqing");
        CHINESE_TO_ENGLISH.put("广西", "Guangxi");
        CHINESE_TO_ENGLISH.put("山西", "Shanxi");
        CHINESE_TO_ENGLISH.put("内蒙古", "Inner Mongolia");
        CHINESE_TO_ENGLISH.put("黑龙江", "Heilongjiang");
        CHINESE_TO_ENGLISH.put("吉林", "Jilin");
        CHINESE_TO_ENGLISH.put("云南", "Yunnan");
        CHINESE_TO_ENGLISH.put("贵州", "Guizhou");
        CHINESE_TO_ENGLISH.put("甘肃", "Gansu");
        CHINESE_TO_ENGLISH.put("海南", "Hainan");
        CHINESE_TO_ENGLISH.put("青海", "Qinghai");
        CHINESE_TO_ENGLISH.put("宁夏", "Ningxia");
        CHINESE_TO_ENGLISH.put("新疆", "Xinjiang");
        CHINESE_TO_ENGLISH.put("西藏", "Tibet");
        CHINESE_TO_ENGLISH.put("天津", "Tianjin");
        CHINESE_TO_ENGLISH.put("台湾", "Taiwan");
        CHINESE_TO_ENGLISH.put("香港", "Hong Kong");
        CHINESE_TO_ENGLISH.put("澳门", "Macau");
        
        // 城市中英文映射
        CHINESE_TO_ENGLISH.put("北京", "Beijing");
        CHINESE_TO_ENGLISH.put("上海", "Shanghai");
        CHINESE_TO_ENGLISH.put("广州", "Guangzhou");
        CHINESE_TO_ENGLISH.put("深圳", "Shenzhen");
        CHINESE_TO_ENGLISH.put("杭州", "Hangzhou");
        CHINESE_TO_ENGLISH.put("南京", "Nanjing");
        CHINESE_TO_ENGLISH.put("苏州", "Suzhou");
        CHINESE_TO_ENGLISH.put("成都", "Chengdu");
        CHINESE_TO_ENGLISH.put("武汉", "Wuhan");
        CHINESE_TO_ENGLISH.put("西安", "Xi'an");
        CHINESE_TO_ENGLISH.put("重庆", "Chongqing");
        CHINESE_TO_ENGLISH.put("天津", "Tianjin");
        CHINESE_TO_ENGLISH.put("青岛", "Qingdao");
        CHINESE_TO_ENGLISH.put("大连", "Dalian");
        CHINESE_TO_ENGLISH.put("厦门", "Xiamen");
        CHINESE_TO_ENGLISH.put("宁波", "Ningbo");
        CHINESE_TO_ENGLISH.put("福州", "Fuzhou");
        CHINESE_TO_ENGLISH.put("无锡", "Wuxi");
        CHINESE_TO_ENGLISH.put("长沙", "Changsha");
        CHINESE_TO_ENGLISH.put("郑州", "Zhengzhou");
        CHINESE_TO_ENGLISH.put("济南", "Jinan");
        CHINESE_TO_ENGLISH.put("哈尔滨", "Harbin");
        CHINESE_TO_ENGLISH.put("沈阳", "Shenyang");
        CHINESE_TO_ENGLISH.put("昆明", "Kunming");
        CHINESE_TO_ENGLISH.put("南昌", "Nanchang");
        CHINESE_TO_ENGLISH.put("合肥", "Hefei");
        CHINESE_TO_ENGLISH.put("石家庄", "Shijiazhuang");
        CHINESE_TO_ENGLISH.put("太原", "Taiyuan");
        CHINESE_TO_ENGLISH.put("兰州", "Lanzhou");
        CHINESE_TO_ENGLISH.put("银川", "Yinchuan");
        CHINESE_TO_ENGLISH.put("西宁", "Xining");
        CHINESE_TO_ENGLISH.put("乌鲁木齐", "Urumqi");
        CHINESE_TO_ENGLISH.put("拉萨", "Lhasa");
        CHINESE_TO_ENGLISH.put("台北", "Taipei");
        CHINESE_TO_ENGLISH.put("高雄", "Kaohsiung");
        CHINESE_TO_ENGLISH.put("香港", "Hong Kong");
        CHINESE_TO_ENGLISH.put("澳门", "Macau");
        
        // 反向映射
        for (Map.Entry<String, String> entry : CHINESE_TO_ENGLISH.entrySet()) {
            ENGLISH_TO_CHINESE.put(entry.getValue(), entry.getKey());
        }
    }

    @Override
    public String translateToEnglish(String chineseText) {
        if (chineseText == null || chineseText.trim().isEmpty()) {
            return "";
        }
        
        String result = CHINESE_TO_ENGLISH.get(chineseText.trim());
        if (result != null) {
            return result;
        }
        
        // 如果没有找到映射，记录日志并返回原文
        log.debug("未找到中文翻译: {}", chineseText);
        return chineseText;
    }

    @Override
    public String translateToChinese(String englishText) {
        if (englishText == null || englishText.trim().isEmpty()) {
            return "";
        }
        
        String result = ENGLISH_TO_CHINESE.get(englishText.trim());
        if (result != null) {
            return result;
        }
        
        // 如果没有找到映射，记录日志并返回原文
        log.debug("未找到英文翻译: {}", englishText);
        return englishText;
    }
}
